<?php //var_dump($this->missingprops);?>
<?php //var_dump($this->freeb); ?>

<?php //set the about URI as in properties.phtml ?>
<span about="<?php echo $this->resourceUri ?>" style="display: none" class="about_span"></span>

<?php /* 'freeb' is a big dirty nested array we walk to print output tables */ ?>
<?php if ($this->has('freeb')): ?>

  <?php //configure our namespaces as in properties.phtml ?>
	<?php $odd = true; $current = 0; $graphCount = count($this->graphs) ?>
	<table class="separated-vertical rdfa" about="<?php echo $this->resourceUri ?>"
	    <?php foreach ($this->namespaces as $namespace => $prefix): ?>
	        <?php echo ' xmlns:' . $prefix . '="' . $namespace . '"' ?>
	    <?php endforeach; ?>>

  <?php foreach($this->freeb as $type_uri => $type_array): /*iterate over each type*/?>
    <?php /* display the type as a grouptitle and create a hidden row for editing types */ ?>
    <tbody id="table-group-<?php echo $current; ?>">
      <div update:from="<?php echo($this->graphUri); ?>">
    	<tr class="grouptitle">
	  	  <th colspan="2">
	  	    <a class="toggle"></a><?php echo($type_array['title']); ?>
	  	  </th>
	  	</tr>
		<tr style="display:none">
		<td><a about="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" href="#" >type</a></td>
			  <td><a rel="<?php echo($this->namespaces['http://www.w3.org/1999/02/22-rdf-syntax-ns#']);?>:type"
	                 resource="<?php echo($type_uri); ?>"></a></td>
		</tr>
	  </div>
	<?php $empty_type = true;?>
    <?php foreach($this->graphs as $graph): /* over each graph */?>
    <?php //echo($graph); ?>
      <?php if(array_key_exists($graph, $type_array)): ?>

    	<div update:from="<?php echo $graph ?>" id="table-group-<?php echo $current ?>">
			<?php foreach($type_array[$graph] as $uri => $predicate): ?>
					  <?php $predicate = $type_array[$graph][$uri]; ?>
					  <?php $empty_type = false;?>
		            	<tr>
			                <td width="25%">
			                    <a class="hasMenu"
			                       about="<?php echo $predicate['uri'] ?>"
			                       href="<?php echo $predicate['url'] ?>"><?php echo $predicate['title'] ?></a>
			            	</td>
		                <?php if (count($this->values[$graph][$uri]) > 1): ?>
							<td>
		                    <ul class="bullets-none">
		                        <?php foreach ($this->values[$graph][$uri] as $entry): ?>
		                            <?php if ($entry['url']): ?>
		                                <li>
		                                    <a resource="<?php echo $entry['uri'] ?>"
		                                       rel="<?php echo $predicate['curi'] ?>"
		                                       class="expandable hasMenu" href="<?php echo $entry['url'] ?>"><?php echo $entry['object'] ?></a>
		                                </li>
		                            <?php else: ?>
		                                <li property="<?php echo $predicate['curi'] ?>"
		                                    content="<?php echo $this->escape($entry['content']) ?>"
		                                    <?php if (isset($entry['lang']) && !empty($entry['lang'])): ?>
		                                        xml:lang="<?php echo $entry['lang'] ?>"
		                                    <?php elseif (isset($entry['datatype']) && !empty($entry['datatype'])): ?>
		                                        datatype="<?php echo $entry['datatype'] ?>"
		                                    <?php endif; ?>
		                                    ><?php
		                                    echo $entry['object']
		                                ?></li>
		                            <?php endif; ?>
		                        <?php endforeach; ?>
		                        <?php if (isset($predicate['has_more']) && $predicate['has_more']): ?>
		                            <a href="<?php echo $predicate['has_more_link'] ?>">[<?php echo $this->_('more') ?>]</a>
		                        <?php endif; ?>
		                    </ul>
		                	</td>
		                <?php else: ?>
		                    <?php $entry = current($this->values[$graph][$uri]) ?>
		                <td>
		                    <?php if ($entry['url']): ?>
		                        <?php if ($entry['uri']): ?>
		                            <a rel="<?php echo $predicate['curi'] ?>"
		                               class="expandable hasMenu"
		                               resource="<?php echo $entry['uri'] ?>"
		                               href="<?php echo $entry['url'] ?>"><?php echo $entry['object'] ?></a>
		                        <?php else: ?>
		                            <a rel="<?php echo $predicate['curi'] ?>"
		                               href="<?php echo $entry['url'] ?>"><?php echo $entry['object'] ?></a>
		                        <?php endif; ?>
		                    <?php else: ?>
		                        <span property="<?php echo $predicate['curi'] ?>"
		                              content="<?php echo $this->escape($entry['content']) ?>"
		                              <?php if (isset($entry['lang']) && !empty($entry['lang'])): ?>
		                                    xml:lang="<?php echo $entry['lang'] ?>"
		                                <?php elseif (isset($entry['datatype']) && !empty($entry['datatype'])): ?>
		                                  datatype="<?php echo $entry['datatype'] ?>"
		                              <?php endif; ?>
		                        ><?php echo $entry['object'] ?></span>
		                    <?php endif; ?>
		                	</td>
		                <?php endif; ?>
		            </tr>
			<?php endforeach; ?>
    	</div>

      <?php endif;?>
    <?php endforeach;?>
    <?php if($empty_type): /*if there are no predicates for this type */?>
    	<tr><td width="25%"></td><td><i><?php echo($this->title);?> is a <?php echo($type_array['title']);?></i></td></tr>
    <?php endif;?>

    <?php /* add a undisplayed row for missing defaultProperties */?>
	<?php if(is_array($this->missingprops) && array_key_exists($type_uri, $this->missingprops)):?>
    	<div update:from="<?php echo($this->graphUri); ?>">
    	<?php foreach($this->missingprops[$type_uri] as $uri=>$mprop):?>
			<tr style="display:none">
			<td><a about="<?php echo($uri); ?>" href="#"><?php echo($mprop['title']);?></a></td>
			<?php if ($mprop['object']): // if it's an object prop ?>
				<td>
				<?php // this doesn't work (line below) TODO: deal w/ object props the right way ?>
				<a rel="<?php echo $this->curie($uri)?>" class="expandable hasMenu" resource=" " href=""></a>
				</td>
			<?php else: // assume it's a literal / data prop ?>
				  <td><span property="<?php echo $this->curie($uri); ?>"
		                    content=""></span></td>
				
			<?php endif; ?>
			</tr>
    	<?php endforeach;?>
    	</div>
    <?php endif;?>
    </tbody>
    <?php $current++;?>
  <?php endforeach; ?>

  <?php /* now add the 'misc' properties not associated w/ a type */ ?>
  <?php $has_misc=false;?>
  <?php foreach ($this->predicates as $graph => $predicatesForGraph): ?>
  	<?php if(!empty($predicatesForGraph)):?>
  		<?php $has_misc = true; ?>
  	<?php endif;?>
  <?php endforeach;?>
  <?php if($has_misc):?>
  <tbody id="table-group-<?php echo $current ?>">
  <tr class="grouptitle">
	    <th colspan="2">
	        <a class="toggle"></a>Miscillaneous Properties
	    </th>
  </tr>
  <?php $empty_type = true; ?>
  <?php foreach ($this->predicates as $graph => $predicatesForGraph): ?>

  	  <div update:from="<?php echo $graph ?>">
        <?php foreach ($predicatesForGraph as $uri => $predicate): ?>
          <?php $empty_type = false; ?>
            <?php $currentPredicate = $this->predicates[$graph][$uri] ?>
            <tr>
                <td width="25%">
                    <a class="hasMenu"
                       about="<?php echo $currentPredicate['uri'] ?>"
                       href="<?php echo $currentPredicate['url'] ?>"><?php echo $currentPredicate['title'] ?></a>
                </td>
                <?php if (count($this->values[$graph][$uri]) > 1): ?>
                <td>
                    <ul class="bullets-none">
                        <?php foreach ($this->values[$graph][$uri] as $entry): ?>
                            <?php if ($entry['url']): ?>
                                <li>
                                    <a resource="<?php echo $entry['uri'] ?>"
                                       rel="<?php echo $currentPredicate['curi'] ?>"
                                       class="expandable hasMenu" href="<?php echo $entry['url'] ?>"><?php echo $entry['object'] ?></a>
                                </li>
                            <?php else: ?>
                                <li property="<?php echo $currentPredicate['curi'] ?>"
                                    content="<?php echo $this->escape($entry['content']) ?>"
                                    <?php if (isset($entry['lang']) && !empty($entry['lang'])): ?>
                                        xml:lang="<?php echo $entry['lang'] ?>"
                                    <?php elseif (isset($entry['datatype']) && !empty($entry['datatype'])): ?>
                                        datatype="<?php echo $entry['datatype'] ?>"
                                    <?php endif; ?>
                                    ><?php
                                    echo $entry['object']
                                ?></li>
                            <?php endif; ?>
                        <?php endforeach; ?>
                        <?php if (isset($currentPredicate['has_more']) && $currentPredicate['has_more']): ?>
                            <a href="<?php echo $currentPredicate['has_more_link'] ?>">[<?php echo $this->_('more') ?>]</a>
                        <?php endif; ?>
                    </ul>
                </td>
                <?php else: ?>
                    <?php $entry = current($this->values[$graph][$uri]) ?>
                <td>
                    <?php if ($entry['url']): ?>
                        <?php if ($entry['uri']): ?>
                            <a rel="<?php echo $currentPredicate['curi'] ?>"
                               class="expandable hasMenu"
                               resource="<?php echo $entry['uri'] ?>"
                               href="<?php echo $entry['url'] ?>"><?php echo $entry['object'] ?></a>
                        <?php else: ?>
                            <a rel="<?php echo $currentPredicate['curi'] ?>"
                               href="<?php echo $entry['url'] ?>"><?php echo $entry['object'] ?></a>
                        <?php endif; ?>
                    <?php else: ?>
                        <span property="<?php echo $currentPredicate['curi'] ?>"
                              content="<?php echo $this->escape($entry['content']) ?>"
                              <?php if (isset($entry['lang']) && !empty($entry['lang'])): ?>
                                    xml:lang="<?php echo $entry['lang'] ?>"
                                <?php elseif (isset($entry['datatype']) && !empty($entry['datatype'])): ?>
                                  datatype="<?php echo $entry['datatype'] ?>"
                              <?php endif; ?>
                        ><?php echo $entry['object'] ?></span>
                    <?php endif; ?>
                </td>
                <?php endif; ?>
            </tr>
        <?php endforeach; ?>
    </div>
    <?php endforeach;?>
    <?php if($empty_type):?>
    	<tr><td width="25%"></td><td><i>no additional properties defined</i></td></tr>
    <?php endif;?>
    </tbody>
  <?php endif; ?>
  </table>

<?php endif; ?>
